<?php

namespace app\hotel\controller;

use think\Controller;

class Business extends Controller
{
    protected $BusinessModel;
    protected $PayModel;

    public function __construct()
    {
        parent::__construct();

        // 公共区域加载模型
        $this->BusinessModel = model('Business.Business');
        $this->PayModel = model('pay.Pay');
    }

    // 注册
    public function register()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            if (empty($mobile) || empty($password)) {
                $this->error('手机号或密码不能为空');
            }

            $business = $this->BusinessModel->where(['mobile' => $mobile])->find();

            if ($business) {
                $this->error('该手机号已被注册');
                exit;
            }

            // 生成一个密码盐
            $salt = randstr();

            // 对密码进行加密
            $password = md5($password . $salt);

            // 组装数据
            $data = [
                'mobile' => $mobile,
                'nickname' => $mobile,
                'password' => $password,
                'salt' => $salt,
                'gender' => '0', //性别
                'deal' => '0', //成交状态
                'money' => '0', //余额
                'auth' => '0', //实名认证
            ];

            // 查询客户来源id
            $sourceid = model('common/Business/Source')->where(['name' => ['LIKE', "%家居商城%"]])->value('id');

            if ($sourceid) {
                $data['sourceid'] = $sourceid;
            }

            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            if ($result === false) {
                // 失败
                $this->error($this->BusinessModel->getError());
                exit;
            } else {
                // 注册成功条状登录页
                $this->success('注册成功', '/business/login');
                exit;
            }
        }
    }

    // 登录
    public function login()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $password = $this->request->param('password', '', 'trim');

        $ckeck = $this->BusinessModel->where(['mobile' => $mobile])->find();

        if (!$ckeck) {
            $this->error('手机号或密码错误');
            exit;
        }

        $salt = $ckeck['salt'];
        $password = md5($password . $salt);

        if ($ckeck['password'] != $password) {
            $this->error('手机号或密码错误');
            exit;
        }

        unset($ckeck['password']);
        unset($ckeck['salt']);

        $this->success('登录成功', '/business/index', $ckeck);
        exit;
    }

    // 验证登录
    public function check()
    {
        $id = $this->request->param('id', '', 'trim');
        $mobile = $this->request->param('mobile', '', 'trim');

        $business = $this->BusinessModel->where(['id' => $id, 'mobile' => $mobile])->find();

        if ($business) {

            unset($business['password']);
            unset($business['salt']);

            $this->success('验证成功', null, $business);

            exit;
        } else {
            $this->error('验证失败');
            exit;
        }
    }

    // 修改个人资料
    public function profile()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', '', 'trim');

            $business = $this->BusinessModel->where(['id' => $id])->find();

            if (empty($business)) {
                $this->error('未知用户');
                exit;
            }

            $data = [
                'id'       => $id,
                'nickname' => $this->request->param('nickname', '', 'trim'),
                'mobile'   => $this->request->param('mobile', '', 'trim'),
                'gender'   => $this->request->param('gender', '', 'trim'),
            ];

            $email = $this->request->param('email', '', 'trim');
            $password = $this->request->param('password', '', 'trim');
            $code = $this->request->param('code', '', 'trim');

            // 如果用户输入了密码，就修改密码
            if (!empty($password)) {
                // 重新生成密码盐
                $salt = randstr();

                $data['salt'] = $salt;
                $data['password'] = md5($password . $salt);
            }

            // 如果用户修改了邮箱，就取消认证
            if ($email != $business['email']) {
                $data['email'] = $email;
                $data['auth'] = '0';
            }
            // 用户是否选择地区
            if (!empty($code)) {
                $parentpath = model('Region')->where(['code' => $code])->value('parentpath');

                if (!empty($parentpath)) {
                    $arr = explode(',', $parentpath);

                    $data['province'] = isset($arr[0]) ? $arr[0] : null;
                    $data['city'] = isset($arr[1]) ? $arr[1] : null;
                    $data['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }
            // 判断是否有图片上传
            if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0) {
                $result = upload_image('avatar');

                if (!$result['success']) {
                    $this->error($result['msg']);
                    exit;
                }

                $data['avatar'] = $result['data'];
            }

            // 更新数据
            $result = $this->BusinessModel->validate('common/Business/Business.profile')->isUpdate(true)->save($data);

            // 写入数据库失败
            if ($result = false) {
                $this->error($this->BusinessModel->getError());
                exit;
            }

            // 判断是否有旧图片，有则删除
            if (isset($data['avatar'])) {
                $old_avatar = '.' . $business['avatar'];
                is_file($old_avatar) && @unlink($old_avatar);
            }

            $business = $this->BusinessModel->where(['id' => $id])->find();

            unset($business['password']);
            unset($business['salt']);

            $this->success('更新资料成功', null, $business);
            exit;
        }
    }
}
